import cv2
import colorList

def get_color1(frame):
    # print('go in get_color')
    height, width, channel = frame.shape
    height_1_2 = int(height/2)
    width_1_2 = int(width/2)

    length = 100
    roi_patch = frame[width_1_2-length:width_1_2+length,height_1_2-length:height_1_2+length]
    point1 = [width_1_2-length,height_1_2-length]
    point2 = [width_1_2+length,height_1_2+length]

    frame = roi_patch
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    maxsum = 0
    color = None
    color_dict = colorList.getColorList()
    for d in color_dict:
        mask = cv2.inRange(hsv, color_dict[d][0], color_dict[d][1])
        binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
        binary = cv2.dilate(binary, None, iterations=2)
        cnts = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
        sum = 0
        for c in cnts:
            sum += cv2.contourArea(c)
        if sum > maxsum:
            maxsum = sum
            color = d
    return color,point1,point2




def get_color(frame):
    # print('go in get_color')
    height, width, channel = frame.shape
    height_1_2 = int(height/2)
    width_1_2 = int(width/2)

    length = 100
    roi_patch = frame[width_1_2-length:width_1_2+length,height_1_2-length:height_1_2+length]
    point1 = [width_1_2-length,height_1_2-length]
    point2 = [width_1_2+length,height_1_2+length]

    color = 'no_move'

    b = roi_patch[:, :, 0]
    g = roi_patch[:, :, 1]
    r = roi_patch[:, :, 2]

    b = b.sum()
    g = g.sum()
    r = r.sum()

    p = 0.5

    if p*r>g and p*r>b:
        color = "red"

    if p*g>r and p*g>b:
        color = "green"

    if b>r and b>g:
        color = "no_move"

    return color, point1, point2

















